package cn.edu.ldu.views.operator;


import cn.edu.ldu.controller.UserController;
import cn.edu.ldu.entity.ChatMemberEntity;
import cn.edu.ldu.entity.GroupChatEntity;
import cn.edu.ldu.protocol.ChatMember;
import cn.edu.ldu.protocol.Protocol;
import cn.edu.ldu.util.Consts;
import cn.edu.ldu.util.L;
import cn.edu.ldu.util.Translate;
import com.alibaba.fastjson.JSON;

import javax.swing.*;
import java.io.IOException;
import java.net.*;

/**
 *
 * @author DYZ
 */
public class AddGroups extends javax.swing.JFrame {

    /**
     * Creates new form AddGroups
     */
    public AddGroups(GroupChatEntity groupChatEntity) {
        initComponents(groupChatEntity);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents(GroupChatEntity groupChatEntity) {

        jPanel1 = new javax.swing.JPanel();
        btnAdd = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtGroupId = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtGroupname = new javax.swing.JTextField();

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        btnAdd.setText("确定添加");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(138, 138, 138)
                                .addComponent(btnAdd)
                                .addContainerGap(132, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(36, 36, 36)
                                .addComponent(btnAdd)
                                .addContainerGap(37, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_END);

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("群聊信息"));

        jLabel1.setText("群聊账户：");

        txtGroupId.setText(groupChatEntity.getId().toString());
//        txtGroupId.setText("342334232");
        txtGroupId.setEnabled(false);

        jLabel2.setText("群聊名称：");

        txtGroupname.setText(groupChatEntity.getGroupName());
//        txtGroupname.setText("23424234");
        txtGroupname.setEnabled(false);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(48, 48, 48)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addGroup(jPanel2Layout.createSequentialGroup()
                                                .addComponent(jLabel2)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(txtGroupname))
                                        .addGroup(jPanel2Layout.createSequentialGroup()
                                                .addComponent(jLabel1)
                                                .addGap(18, 18, 18)
                                                .addComponent(txtGroupId, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addContainerGap(57, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(32, 32, 32)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel1)
                                        .addComponent(txtGroupId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(38, 38, 38)
                                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(jLabel2)
                                        .addComponent(txtGroupname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addContainerGap(44, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel2, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>

    /**
     * 添加按钮
     * 1、询问是否添加
     * @param evt
     */
    private void btnAddActionPerformed(java.awt.event.ActionEvent evt){
        int res = JOptionPane.showConfirmDialog(null, "是否添加到该群聊", "是否继续", JOptionPane.YES_NO_OPTION);
        if (res == JOptionPane.YES_OPTION) {
            // 点击“是”后执行这个代码块
            //提交
            String groupId = txtGroupId.getText();
            String groupName = txtGroupname.getText();

            InetAddress remoteAddr = null;

            try {
                remoteAddr = InetAddress.getByName(Consts.ipv4);
                DatagramSocket clientSocket = null;
                clientSocket = new DatagramSocket();

                Protocol protocol = new Protocol();
                protocol.setKey("AddGroup");
                protocol.setToken(UserController.getToken());

                ChatMemberEntity chatMemberEntity = new ChatMemberEntity();
                chatMemberEntity.setGroupId(Long.valueOf(groupId));
                chatMemberEntity.setId(UserController.getUserEntity().getId());

                protocol.setData(JSON.toJSONString(chatMemberEntity));
                byte[] data = Translate.ObjectToByte(protocol);

                DatagramPacket packet = new DatagramPacket(data,data.length,remoteAddr,Consts.port);
                clientSocket.send(packet);
                L.OK(packet,AddGroups.class);

                Protocol pro = (Protocol) Translate.ByteToObject(packet.getData());
                L.OK(pro,AddGroups.class);

                pro.key();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //提交失败
        } else {
            return;
        }
    }

    private void formWindowClosed(java.awt.event.WindowEvent evt) {
        // TODO add your handling code here:
        this.dispose();
        //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    // Variables declaration - do not modify
    private javax.swing.JButton btnAdd;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField txtGroupId;
    private javax.swing.JTextField txtGroupname;
    // End of variables declaration
}
